/*
 * ============================================================================
 *
 *       Filename:  aplua.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015年03月02日 15时39分12秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * ============================================================================
 */
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>

#include "config.h"
#include "aplua.h"
#include "log.h"

struct lua_handle_t luahandle = {0};

static void 
aplua_handle_init(lua_State **pL, char *script)
{
	assert(*pL == NULL);
	*pL = luaL_newstate();
	assert(*pL != NULL);
	luaL_openlibs(*pL);

	if(luaL_loadfile(*pL, script)) {
		sys_err("%s\n", lua_tostring(*pL, -1));
		exit(-1);
	}

	if(lua_pcall(*pL, 0, 0, 0)) {
		sys_err("%s\n", lua_tostring(*pL, -1));
		exit(-1);
	}
}

static void aplua_handle_free(lua_State *L)
{
	assert(L != NULL);
	lua_close(L);
}

void aplua_init()
{
	aplua_handle_init(&luahandle.L_apstatus, LUA_APSTATUS);
	sys_info("MODULE: lua init success\n");
}

void aplua_free()
{
	aplua_handle_free(luahandle.L_apstatus);
}

